package cjh.com.myiocannotation;

/*
 * author: caojunhao
 * date  : 2020/11/25
 * desc  :
 */

import android.util.Log;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 事件代理类
 * public void onClick(View v){}
 */
public class ListenerInvocationHandler implements InvocationHandler {
    private Object activity;
    private Method activityMethod;

    public ListenerInvocationHandler(Object activity, Method activityMethod) {
        this.activity = activity;
        this.activityMethod = activityMethod;
    }

    /**
     * 点击按钮后执行这个方法
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //在这里调用被注解的onClick方法
        Log.d("ListenerInvocat", "invoke method==="+method);
        //method=abstract void android.view.View$OnClickListener.onClick(android.view.View)
        return activityMethod.invoke(activity, args);
    }
}
